package com.shire.shire_interface.controller.marketing;

import com.alibaba.fastjson.JSONObject;
import com.shire.shire_interface.common.bin.Response;
import com.shire.shire_interface.controller.BaseController;
import com.shire.shire_interface.model.group.WxGroup;
import com.shire.shire_interface.model.group.WxGroupExample;
import com.shire.shire_interface.model.marketing.WxGroupAward;
import com.shire.shire_interface.model.marketing.WxGroupAwardExample;
import com.shire.shire_interface.model.marketing.WxGroupAwardWithBLOBs;
import com.shire.shire_interface.service.group.WxGroupService;
import com.shire.shire_interface.service.marketing.WxGroupAwardService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.*;

@RestController
@RequestMapping("/award")
public class WxGroupAwardController extends BaseController {
    @Resource
    private WxGroupAwardService groupAwardService;
    @Resource
    private WxGroupService groupService;

    @ResponseBody
    @PostMapping("/saveGroupAward")
    public Response insetGroupAward(@RequestBody WxGroupAwardWithBLOBs groupAward, HttpSession session){
        Response response = new Response();
        try {

            if (groupAward.getId() != null) {
                groupAwardService.updateByPrimaryKey(groupAward);
            }
            else {
                groupAward.setCreatetime(new Date());
                groupAwardService.insetGroupAward(groupAward);
            }

            response.setData(groupAward);
            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }

    @ResponseBody
    @PostMapping("/deleteAward")
    public Response del(@RequestBody WxGroupAward groupAward){
        Response response = new Response();
        try {
            groupAwardService.deleteByPrimaryKey(groupAward.getId());
            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }

    @ResponseBody
    @PostMapping("/getGroupAwardList")
    public Response getGroupAwardList(@RequestBody JSONObject jsonObject, HttpSession session){
        Response response = new Response();
        try {
            Integer csId = jsonObject.getInteger("csId");

            if (!checkLoginUserByCsId(session, csId)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }

            WxGroupAwardExample example = new WxGroupAwardExample();

            String wxAccount = jsonObject.getString("wxaccount");


            example.createCriteria().andWxaccountEqualTo(wxAccount);
            example.setOrderByClause("createtime desc");

            addPageParamToExample(jsonObject,example);

            List<WxGroupAwardWithBLOBs> groupAwardList = groupAwardService.getGroupAwardHistory(example);

            HashSet<String> groupIdSet = new HashSet<>();

            for (WxGroupAwardWithBLOBs award : groupAwardList){
                List<String> groupIds = Arrays.asList(award.getTaskgroup().split(","));
                award.setGrouplist(groupIds);
                groupIdSet.addAll(groupIds);
                award.setGroups(new ArrayList<>());
            }

            if (groupIdSet.size()>0) {
                WxGroupExample groupExample = new WxGroupExample();
                List<String> groupIdList = new ArrayList<>();
                Iterator<String> it = groupIdSet.iterator();
                while (it.hasNext()){
                    groupIdList.add(it.next());
                }
                groupExample.createCriteria().andWxaccountEqualTo(wxAccount).andGroupnoIn(groupIdList);

                List<WxGroup> groupList = groupService.getAllGroups(groupExample);

                for (WxGroup group : groupList){
                    for (WxGroupAward award : groupAwardList){
                        if (award.getGrouplist().contains(group.getGroupno())){
                            award.getGroups().add(group);
                        }
                    }
                }

            }
            response.setData(groupAwardList);
            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }
}
